//ajax 请求的根路径

const ajaxBaseUrl = '//localhost:51537' // '//192.168.1.137:7909'


/**
 * 设置 cookie
 * @param {string} cname
 * @param {string} cvalue
 * @param {number} exdays
 */
function setCookie(cname, cvalue, exdays) {
	var d = new Date()
	d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000))
	var expires = "expires=" + d.toGMTString();
	document.cookie = cname + "=" + cvalue + "; " + expires
}

/**
 * 获取 cookie 中传递的参数的值
 * @param {string} cname
 * @returns
 */
function getCookie(cname) {
	var name = cname + "="
	var ca = document.cookie.split(';')
	for (var i = 0; i < ca.length; i++) {
		var c = ca[i].trim()
		if (c.indexOf(name) == 0) {
			return c.substring(name.length, c.length)
		}
	}
	return ""
}

/**
 * 获取 localStorage 中的值
 * @param {string} key
 */
function getLocalStorage(key) {
	return window.localStorage ? localStorage.getItem(key) : getCookie(key)
}

/**
 * 设置 localStorage 中的值
 * @param {string} key
 * @param {string} value
 */
function setLocalStorage(key, value) {
	window.localStorage ? localStorage.setItem(key, value) : setCookie(key, value, 1)
}

/**
 * 设置 sessionStorage
 * @param {string} key
 * @param {string} value
 */
function setSession(key, value) {
	if (window.sessionStorage) {
		sessionStorage.setItem(key, value)
	} else if (window.localStorage) {
		setLocalStorage(key, value)
	} else {
		setCookie(key, value, 1)
	}
}

/**
 * 获取 sessionStorage 中传递的参数的值
 * @param {string} key
 * @returns
 */
function getSession(key) {
	var value
	if (window.sessionStorage) {
		value = sessionStorage.getItem(key)
	} else if (window.localStorage) {
		value = getlocalStorage(key)
	} else {
		value = getCookie(key)
	}
	return value
}

/**
 * 判断是否登录, 如未登录页面调整的参数页
 * @param {string} url
 */
function login(url) {
	location.href = url
}

/**
 * 获取 location.search 中的参数值
 * @param {string} name
 * @returns
 */
function getUrlParam(name) {
	var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
	var r = window.location.search.substr(1).match(reg);
	if (r != null) {
		return decodeURIComponent(r[2]);
	} else {
		return null;
	}
}

/**
 * 获取 url 中参数的值并赋值给对应的元素
 * @param {string} name
 */
function setText(name) {
	let obj = JSON.parse(getLocalStorage('searchKey'))
	$('#' + name).text(obj[name])
}
/**
 * 格式化时间
 * @param {string} fmt 时间格式
 * @returns {string}
 */
//window.Date.prototype.format = function (fmt = 'YYYY-MM-DD HH:mm:ss') {
window.Date.prototype.format = function (fmt = 'YYYY-MM-DD') {
	const map = {
		'M+': this.getMonth() + 1,
		'D+': this.getDate(),
		'd+': this.getDate(),
		'H+': this.getHours(),
		'm+': this.getMinutes(),
		's+': this.getSeconds(),
		'q+': Math.floor((this.getMonth() + 3) / 3),
		'S': this.getMilliseconds()
	}
	if (/(y+)/i.test(fmt)) {
		let result = RegExp.$1
		fmt = fmt.replace(result, this.getFullYear().toString().substr(4 - result.length))
	}
	for (let k in map) {
		if (new RegExp('(' + k + ')').test(fmt)) {
			let result = RegExp.$1
			fmt = fmt.replace(result, map[k].toString().padStart(result.length, '0'))
		}
	}
	return fmt
}

/**
 * 发生 ajax post 请求
 * @param {object} obj
 */
function post(obj) { 
	$.ajax({
		url: ajaxBaseUrl + obj.url,
		type: 'post',
		dataType: 'json',
		data: obj.data,
		beforeSend: function (xhr) {
			xhr.setRequestHeader('SiteApp.Web', 'qDV0Dyovqz+76NoaeCXucw==')
		},
		success: (data) => {
			if (data.returnCode === 200 && data.data != null) {
				obj.success(data.data)
			}
		},
		error: (data) => {
			if(obj.error) obj.error(data)
		}
	})
}